package kr.re.etri.snsenabler.client;

import kr.re.etri.snsenabler.client.auth.AbstractAuthorizationDialog;
import kr.re.etri.snsenabler.shared.Data;
import kr.re.etri.snsenabler.shared.SocialServiceProvider;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ImageResource;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.button.ToolButton;
import com.sencha.gxt.widget.core.client.container.AccordionLayoutContainer.AccordionLayoutAppearance;

class ServiceProviderPanel extends ContentPanel {

	private final Data user;
	private final SocialServiceProvider socialServiceProvider;
	@SuppressWarnings("unused") private final boolean isUsing;
	private ToolButton authorizeToolButton;

	public ServiceProviderPanel(AccordionLayoutAppearance appearance, Data user, SocialServiceProvider socialServiceProvider, boolean isUsing, ImageResource icon) {
		super(appearance);

		this.user = user;
		this.socialServiceProvider = socialServiceProvider;
		this.isUsing = isUsing;

		getHeader().setIcon(icon);
		addTool(getAuthorizeToolButton());
	}

	private ToolButton getAuthorizeToolButton() {
		if (authorizeToolButton == null) {
			authorizeToolButton = new ToolButton(ToolButton.REFRESH, new ClickHandler() {
				public void onClick(ClickEvent event) {
					authorize();
				}
			});
		}
		return authorizeToolButton;
	}

	private void authorize() {
		AbstractAuthorizationDialog dialog = AbstractAuthorizationDialog.createDialog(user, socialServiceProvider);
		dialog.getDialog().show();
	}
}
